package chessGame;

import chessGame.piece.Bishop;
import chessGame.piece.Knight;
import chessGame.piece.Piece;
import chessGame.piece.Queen;
import chessGame.utils.ReflectPiece;
import chessLib.Position;

import java.util.*;

public class ComplexGame extends BaseGame {

    private Board board;
    private Random random;
    private List<Piece> pieces;

    private static final String PIECE_PACKAGE = "chessGame.piece";
    public ComplexGame() {
        this.board = new Board();
        this.random = new Random();
        this.pieces = new ArrayList<>();
    }
    HashMap<String, Position> piecePositions = new HashMap<String, Position>(){{
        put("Rook", new Position(1, 1));
        put("Knight", new Position(1, 2));
        put("Bishop", new Position(1, 3));
        put("Queen", new Position(1, 4));
        put("King", new Position(1, 5));
        put("Pawn", new Position(1, 6));
    }};

    @Override
    public void setup() {
        // Clear any existing pieces
        board = new Board();
        pieces.clear();

        //初始化棋子位置
        pieces.addAll(ReflectPiece.scanPiece(PIECE_PACKAGE));
        // 初始化三个棋子的位置
        for (Piece piece : pieces) {
            board.placePiece(piece, piecePositions.get(piece.getName()));
        }
        /*Knight knight = new Knight();
        board.placePiece(knight, new Position(1, 1));
        pieces.add(knight);

        Bishop bishop = new Bishop();
        board.placePiece(bishop, new Position(1, 2));
        pieces.add(bishop);

        Queen queen = new Queen();
        board.placePiece(queen, new Position(1, 3));
        pieces.add(queen);*/
    }
    @Override
    public void play(int moves) {
        System.out.println("Initial board setup:");
        //打印一个8 * 8的棋盘;
        printBoard();

        for (int i = 0; i < moves; i++) {
            System.out.println("\nMove " + (i + 1) + ":");
            makeRandomMove();
            printBoard();
        }
    }

    private void makeRandomMove() {
        if (pieces.isEmpty()) {
            return;
        }

        // 随意选一个棋子进行移动
        Piece selectedPiece = pieces.get(random.nextInt(pieces.size()));

        // 找到所以棋子在棋盘上的位置
        Position piecePosition = null;
        for (Map.Entry<Position, Piece> entry : board.getAllPieces()) {
            if (entry.getValue() == selectedPiece) {
                piecePosition = entry.getKey();
                break;
            }
        }

        if (piecePosition == null) {
            // 在棋盘上没有这个棋子
            return;
        }

        // 获取所选棋子的有效移动
        Set<Position> validMoves = selectedPiece.getValidMoves(piecePosition, board);
        //如果没有有效移动，则返回
        if (validMoves.isEmpty()) {
            System.out.println("No valid moves for " + selectedPiece.getName() + " at " + piecePosition);
            return;
        }

        // 随机选择一个有效的移动
        List<Position> movesList = new ArrayList<>(validMoves);
        Position targetPosition = movesList.get(random.nextInt(movesList.size()));

        // 移动棋子
        board.movePiece(piecePosition, targetPosition);

        System.out.println(selectedPiece.getName() + " moved from " + piecePosition + " to " + targetPosition);
    }

    private void printBoard() {
        // 使用数组表示棋盘
        String[][] boardRepresentation = new String[8][8];

        // 初始化空棋盘
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                boardRepresentation[i][j] = ".";
            }
        }

        // 棋子在棋盘上的位置
        for (Map.Entry<Position, Piece> entry : board.getAllPieces()) {
            Position pos = entry.getKey();
            Piece piece = entry.getValue();
            String symbol = piece.getName().substring(0, 1);
            boardRepresentation[pos.getRow() - 1][pos.getCol() - 1] = symbol;
        }

        // 打印棋盘
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                System.out.print(boardRepresentation[i][j] + " ");
            }
            System.out.println();
        }
    }
}